package com.kb.design.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kb.design.constant.Constants;
import com.kb.design.entity.*;
import com.kb.design.enums.DesignStatus;
import com.kb.design.enums.FileType;
import com.kb.design.mapper.postgreSQL.*;
import com.kb.design.service.DesignEncapsulationServer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @Auther:chenzx
 * @Date:2023/11/13
 * @Description:
 */
@Service
@DS("slave-1")
@DSTransactional
public class DesignEncapsulationImpl implements DesignEncapsulationServer {

    @Autowired
    private DesignEncapsulationMapper designEncapsulationMapper;
    @Resource
    private ProjectServiceImpl projectServiceImpl;
    @Resource
    private DesignRequirementMapper designRequirementMapper;
    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private DesignFileMapper designFileMapper;
    @Resource
    private DesignResponsiblePersonMapper responsiblePersonMapper;


    @Override
    @DSTransactional
    public void submit(DesignEncapsulation designEncapsulation) {
        designEncapsulation.setCompleteDate(LocalDateTime.now());

        designEncapsulationMapper.updateById(designEncapsulation);
        //子节点只需要更改封装数据即可
        if (Objects.equals(designEncapsulation.getParentId(),1L)){
            return;
        }
        //更新 design_responsible_person的备注信息
        if (StringUtils.isNoneBlank(designEncapsulation.getRemark())){
            DesignResponsiblePerson designResponsiblePerson = new DesignResponsiblePerson();
            designResponsiblePerson.setRemark(designEncapsulation.getRemark());
            LambdaQueryWrapper<DesignResponsiblePerson> queryWrapper = Wrappers.lambdaQuery();
//            queryWrapper.eq(DesignResponsiblePerson::getCommonId,designEncapsulation.getId());
            responsiblePersonMapper.update(designResponsiblePerson,queryWrapper);
        }

        //附件
        if (StringUtils.isNoneBlank(designEncapsulation.getEncapsulationUrl())){
            DesignFile designFile = new DesignFile();
            designFile.setType(DesignStatus.ENCAPSULATION.getIndex());
            designFile.setDesignId(designEncapsulation.getId());
            designFile.setUrl(designEncapsulation.getEncapsulationUrl());
            designFileMapper.insert(designFile);
        }
        //状态流转
        DesignStatusTransfer designStatusTransfer = new DesignStatusTransfer();
        designStatusTransfer.setStatus("2");
        designStatusTransfer.setTitle("封装与网表");
        designStatusTransfer.setProjectId(designEncapsulation.getProjectId());
        designStatusTransfer.setColor("success");

        projectServiceImpl.insertStatusTransfer(designStatusTransfer);

        //生成设计任务
        String designNumber = designRequirementMapper.getDesignNumber();
        DesignRequirement designRequirement = new DesignRequirement();
        designRequirement.setCreateBy(designEncapsulation.getUpdateBy());
        designRequirement.setCreateByName(designEncapsulation.getUpdateByName());
        designRequirement.setProjectId(designEncapsulation.getProjectId());
        designRequirement.setProjectName(designEncapsulation.getProjectName());
//        designRequirement.setDesignStatus(DesignStatus.AWAIT_ASSIGN.getIndex());
        designRequirementMapper.insert(designRequirement);

        //修改项目表里面的 设计状态
        DesignProject designProject = new DesignProject();
        
        designProject.setId(designEncapsulation.getProjectId());
//        designProject.setNoteStatus(DesignStatus.AWAIT_ASSIGN.getIndex());
        projectMapper.updateById(designProject);
    }
}
